<?php
class UserCreditModel extends Model {
	
	var $tableName	=	'user_credit';
	protected $credit_setting	=	false;
	protected $fields	=	array (0 => 'id',1 => 'uid',2 => 'info',3 => 'action',4 => 'type',5 => 'credit',6 => 'mtime','_autoinc' => true,'_pk' => 'id');

	// 获取用户的积分记录，uid，开始时间，结束时间
	public function get($uid,$pre_page=20){
		
		return $this->where("uid=$uid")->findPage($per_page);
	}
	
	public function setUserScore($uid=0,$action,$credit=0){
		return $this->set($uid,$action,array('score'=>$credit));
	}

	public function setUserExp($uid=0,$action,$credit=0){
		return $this->set($uid,$action,array('exp'=>$credit));
	}
	
	// 为用户设置积分，uid，action，array( 类型 => 分值 ) 只有在配置中credit设置了$的才生效
	public function set($uid=0,$action,$credit=false){
		
		if(!$uid){
			//错误的用户ID
			return -1;
		}else{
			if(false == $credit){
				$credit=array('exp'=>"",'score'=>"");
			}
			foreach($credit as $k=>$v){
				$this->setCreditLog($uid,$action,$k,$v);
			}
		}
	}

	// 更新用户积分情况
	public function setCreditLog($uid,$action,$type,$credit=0){
		//获取积分配置
		$setting		=	$this->getCreditSetting($uid,$action);
		$map['uid']		=	$uid;
		$map['mtime']	=	time();
		$map['action']	=	$action;
		$map['type']	=	$type;
		//增加积分记录
		if($setting[$type]=='$'){
			$map['credit']	=	$credit;
		}else{
			$map['credit']	=	$setting[$type];
		}
		//解析info中的模板
		$map['info']	=	$setting['info'];
		//'执行'.$setting['actioncn'].'，赚了'.$setting[$type].$type;
		if(empty($map['credit'])) return false;
		$result	=	$this->add($map);
		if($result){
			return $this->setUserCredit($uid,array($type=>$map['credit']));
		}else{
			return false;
		}
	}

	// 获取用户积分情况,type score|exp (积分|经验)
	public function getUserCredit($uid,$type='all'){
		
		$user	=	model('Space');
		
		$info	=	$user->field('credit_score,credit_exp')->find($uid);
		switch($type){
			case	'exp'	:   $credit	=	$info['credit_exp']; break;
			case	'score'	:	$credit	=	$info['credit_score']; break;
			case	'all'	:	$credit	=	$info;break;
		}
		return $credit;
	}

	// 更新用户积分情况
	public function setUserCredit($uid,$credit=array()){
		$user	=	model('Space');
		
		$info	=	$user->field('credit_score,credit_exp')->find($uid);

		$save_credit['credit_exp']		=	$info['credit_exp']		+	$credit['exp'];
		$save_credit['credit_score']	=	$info['credit_score']	+	$credit['score'];

		$map['uid']	=	$uid;
		$info		=	$user->where($map)->save($save_credit);
		return $info;
	}

	// 获取积分设置
	public function getCreditSetting($uid,$action='') {
		
		// TODO 根据用户的组别，获取不同的积分设置

		// 载入积分配置信息
//		$f_path	=	SITE_PATH.'/_runtime/_cache/';
//
//		if(file_exists($f_path.'_option_user_credit.php')){
//			$this->credit_setting	=	F('_option_user_credit');
//		}else{
			$this->credit_setting	=	model('Xdata')->lget('user_credit');
//			F('_option_user_credit',$this->credit_setting,$f_path);
//		}

		if($action){
			return $this->credit_setting[$action];	
		}else{
			return $this->credit_setting;
		}
	}
	
	// 导入积分设置, setCreditSetting($key,$value);
	/**
	$key	=	ask_post_question_score ,
	$value= array(
		'action'=>'ask_post_question_score',
		'score'=>'$',
		'exp'=> 0,
		'title'=>'发问提悬赏分',
		'info'=>'{actor}悬赏{credit}金币提出一个问题'
	);
	**/
	public function setCreditSetting($action,$data,$replace=false){
		$xdata	=	service('Xdata');
		return $xdata->put('user_credit:'.$action,$data,$replace);
	}
}
?>